/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Taxonomicelement;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Taxonrank;
import entityBeans.Taxonomicstatus;
import entityBeans.Taxonconcept;
import entityBeans.Subgenus;
import entityBeans.Specificepithet;
import entityBeans.Scientificnameauthorship;
import entityBeans.Scientificname;
import entityBeans.Acceptednameusage;
import entityBeans.Class;
import entityBeans.Family;
import entityBeans.Genus;
import entityBeans.Infraspecificepithet;
import entityBeans.Kingdom;
import entityBeans.Nameaccordingto;
import entityBeans.Namepublishedin;
import entityBeans.Nomenclaturalcode;
import entityBeans.Order1;
import entityBeans.Originalnameusage;
import entityBeans.Parentnameusage;
import entityBeans.Phylum;
import entityBeans.Specimen;
import java.util.ArrayList;
import java.util.List;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class TaxonomicelementJpaController {

    public TaxonomicelementJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Taxonomicelement taxonomicelement) {
        if (taxonomicelement.getSpecimenList() == null) {
            taxonomicelement.setSpecimenList(new ArrayList<Specimen>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Taxonrank taxonrank = taxonomicelement.getTaxonrank();
            if (taxonrank != null) {
                taxonrank = em.getReference(taxonrank.getClass(), taxonrank.getIdtaxonrank());
                taxonomicelement.setTaxonrank(taxonrank);
            }
            Taxonomicstatus taxonomicstatus = taxonomicelement.getTaxonomicstatus();
            if (taxonomicstatus != null) {
                taxonomicstatus = em.getReference(taxonomicstatus.getClass(), taxonomicstatus.getIdtaxonomicstatus());
                taxonomicelement.setTaxonomicstatus(taxonomicstatus);
            }
            Taxonconcept taxonconcept = taxonomicelement.getTaxonconcept();
            if (taxonconcept != null) {
                taxonconcept = em.getReference(taxonconcept.getClass(), taxonconcept.getIdtaxonconcept());
                taxonomicelement.setTaxonconcept(taxonconcept);
            }
            Subgenus subgenus = taxonomicelement.getSubgenus();
            if (subgenus != null) {
                subgenus = em.getReference(subgenus.getClass(), subgenus.getIdsubgenus());
                taxonomicelement.setSubgenus(subgenus);
            }
            Specificepithet specificepithet = taxonomicelement.getSpecificepithet();
            if (specificepithet != null) {
                specificepithet = em.getReference(specificepithet.getClass(), specificepithet.getIdspecificepithet());
                taxonomicelement.setSpecificepithet(specificepithet);
            }
            Scientificnameauthorship scientificnameauthorship = taxonomicelement.getScientificnameauthorship();
            if (scientificnameauthorship != null) {
                scientificnameauthorship = em.getReference(scientificnameauthorship.getClass(), scientificnameauthorship.getIdscientificnameauthorship());
                taxonomicelement.setScientificnameauthorship(scientificnameauthorship);
            }
            Scientificname scientificname = taxonomicelement.getScientificname();
            if (scientificname != null) {
                scientificname = em.getReference(scientificname.getClass(), scientificname.getIdscientificname());
                taxonomicelement.setScientificname(scientificname);
            }
            Acceptednameusage acceptednameusage = taxonomicelement.getAcceptednameusage();
            if (acceptednameusage != null) {
                acceptednameusage = em.getReference(acceptednameusage.getClass(), acceptednameusage.getIdacceptednameusage());
                taxonomicelement.setAcceptednameusage(acceptednameusage);
            }
            Class class1 = taxonomicelement.getClass1();
            if (class1 != null) {
                class1 = em.getReference(class1.getClass(), class1.getIdclass());
                taxonomicelement.setClass1(class1);
            }
            Family family = taxonomicelement.getFamily();
            if (family != null) {
                family = em.getReference(family.getClass(), family.getIdfamily());
                taxonomicelement.setFamily(family);
            }
            Genus genus = taxonomicelement.getGenus();
            if (genus != null) {
                genus = em.getReference(genus.getClass(), genus.getIdgenus());
                taxonomicelement.setGenus(genus);
            }
            Infraspecificepithet infraspecificepithet = taxonomicelement.getInfraspecificepithet();
            if (infraspecificepithet != null) {
                infraspecificepithet = em.getReference(infraspecificepithet.getClass(), infraspecificepithet.getIdinfraspecificepithet());
                taxonomicelement.setInfraspecificepithet(infraspecificepithet);
            }
            Kingdom kingdom = taxonomicelement.getKingdom();
            if (kingdom != null) {
                kingdom = em.getReference(kingdom.getClass(), kingdom.getIdkingdom());
                taxonomicelement.setKingdom(kingdom);
            }
            Nameaccordingto nameaccordingto = taxonomicelement.getNameaccordingto();
            if (nameaccordingto != null) {
                nameaccordingto = em.getReference(nameaccordingto.getClass(), nameaccordingto.getIdnameaccordingto());
                taxonomicelement.setNameaccordingto(nameaccordingto);
            }
            Namepublishedin namepublishedin = taxonomicelement.getNamepublishedin();
            if (namepublishedin != null) {
                namepublishedin = em.getReference(namepublishedin.getClass(), namepublishedin.getIdnamepublishedin());
                taxonomicelement.setNamepublishedin(namepublishedin);
            }
            Nomenclaturalcode nomenclaturalcode = taxonomicelement.getNomenclaturalcode();
            if (nomenclaturalcode != null) {
                nomenclaturalcode = em.getReference(nomenclaturalcode.getClass(), nomenclaturalcode.getIdnomenclaturalcode());
                taxonomicelement.setNomenclaturalcode(nomenclaturalcode);
            }
            Order1 order1 = taxonomicelement.getOrder1();
            if (order1 != null) {
                order1 = em.getReference(order1.getClass(), order1.getIdorder());
                taxonomicelement.setOrder1(order1);
            }
            Originalnameusage originalnameusage = taxonomicelement.getOriginalnameusage();
            if (originalnameusage != null) {
                originalnameusage = em.getReference(originalnameusage.getClass(), originalnameusage.getIdoriginalnameusage());
                taxonomicelement.setOriginalnameusage(originalnameusage);
            }
            Parentnameusage parentnameusage = taxonomicelement.getParentnameusage();
            if (parentnameusage != null) {
                parentnameusage = em.getReference(parentnameusage.getClass(), parentnameusage.getIdparentnameusage());
                taxonomicelement.setParentnameusage(parentnameusage);
            }
            Phylum phylum = taxonomicelement.getPhylum();
            if (phylum != null) {
                phylum = em.getReference(phylum.getClass(), phylum.getIdphylum());
                taxonomicelement.setPhylum(phylum);
            }
            List<Specimen> attachedSpecimenList = new ArrayList<Specimen>();
            for (Specimen specimenListSpecimenToAttach : taxonomicelement.getSpecimenList()) {
                specimenListSpecimenToAttach = em.getReference(specimenListSpecimenToAttach.getClass(), specimenListSpecimenToAttach.getIdspecimen());
                attachedSpecimenList.add(specimenListSpecimenToAttach);
            }
            taxonomicelement.setSpecimenList(attachedSpecimenList);
            em.persist(taxonomicelement);
            if (taxonrank != null) {
                taxonrank.getTaxonomicelementList().add(taxonomicelement);
                taxonrank = em.merge(taxonrank);
            }
            if (taxonomicstatus != null) {
                taxonomicstatus.getTaxonomicelementList().add(taxonomicelement);
                taxonomicstatus = em.merge(taxonomicstatus);
            }
            if (taxonconcept != null) {
                taxonconcept.getTaxonomicelementList().add(taxonomicelement);
                taxonconcept = em.merge(taxonconcept);
            }
            if (subgenus != null) {
                subgenus.getTaxonomicelementList().add(taxonomicelement);
                subgenus = em.merge(subgenus);
            }
            if (specificepithet != null) {
                specificepithet.getTaxonomicelementList().add(taxonomicelement);
                specificepithet = em.merge(specificepithet);
            }
            if (scientificnameauthorship != null) {
                scientificnameauthorship.getTaxonomicelementList().add(taxonomicelement);
                scientificnameauthorship = em.merge(scientificnameauthorship);
            }
            if (scientificname != null) {
                scientificname.getTaxonomicelementList().add(taxonomicelement);
                scientificname = em.merge(scientificname);
            }
            if (acceptednameusage != null) {
                acceptednameusage.getTaxonomicelementList().add(taxonomicelement);
                acceptednameusage = em.merge(acceptednameusage);
            }
            if (class1 != null) {
                class1.getTaxonomicelementList().add(taxonomicelement);
                class1 = em.merge(class1);
            }
            if (family != null) {
                family.getTaxonomicelementList().add(taxonomicelement);
                family = em.merge(family);
            }
            if (genus != null) {
                genus.getTaxonomicelementList().add(taxonomicelement);
                genus = em.merge(genus);
            }
            if (infraspecificepithet != null) {
                infraspecificepithet.getTaxonomicelementList().add(taxonomicelement);
                infraspecificepithet = em.merge(infraspecificepithet);
            }
            if (kingdom != null) {
                kingdom.getTaxonomicelementList().add(taxonomicelement);
                kingdom = em.merge(kingdom);
            }
            if (nameaccordingto != null) {
                nameaccordingto.getTaxonomicelementList().add(taxonomicelement);
                nameaccordingto = em.merge(nameaccordingto);
            }
            if (namepublishedin != null) {
                namepublishedin.getTaxonomicelementList().add(taxonomicelement);
                namepublishedin = em.merge(namepublishedin);
            }
            if (nomenclaturalcode != null) {
                nomenclaturalcode.getTaxonomicelementList().add(taxonomicelement);
                nomenclaturalcode = em.merge(nomenclaturalcode);
            }
            if (order1 != null) {
                order1.getTaxonomicelementList().add(taxonomicelement);
                order1 = em.merge(order1);
            }
            if (originalnameusage != null) {
                originalnameusage.getTaxonomicelementList().add(taxonomicelement);
                originalnameusage = em.merge(originalnameusage);
            }
            if (parentnameusage != null) {
                parentnameusage.getTaxonomicelementList().add(taxonomicelement);
                parentnameusage = em.merge(parentnameusage);
            }
            if (phylum != null) {
                phylum.getTaxonomicelementList().add(taxonomicelement);
                phylum = em.merge(phylum);
            }
            for (Specimen specimenListSpecimen : taxonomicelement.getSpecimenList()) {
                Taxonomicelement oldTaxonomicelementOfSpecimenListSpecimen = specimenListSpecimen.getTaxonomicelement();
                specimenListSpecimen.setTaxonomicelement(taxonomicelement);
                specimenListSpecimen = em.merge(specimenListSpecimen);
                if (oldTaxonomicelementOfSpecimenListSpecimen != null) {
                    oldTaxonomicelementOfSpecimenListSpecimen.getSpecimenList().remove(specimenListSpecimen);
                    oldTaxonomicelementOfSpecimenListSpecimen = em.merge(oldTaxonomicelementOfSpecimenListSpecimen);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Taxonomicelement taxonomicelement) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Taxonomicelement persistentTaxonomicelement = em.find(Taxonomicelement.class, taxonomicelement.getIdtaxonomicelement());
            Taxonrank taxonrankOld = persistentTaxonomicelement.getTaxonrank();
            Taxonrank taxonrankNew = taxonomicelement.getTaxonrank();
            Taxonomicstatus taxonomicstatusOld = persistentTaxonomicelement.getTaxonomicstatus();
            Taxonomicstatus taxonomicstatusNew = taxonomicelement.getTaxonomicstatus();
            Taxonconcept taxonconceptOld = persistentTaxonomicelement.getTaxonconcept();
            Taxonconcept taxonconceptNew = taxonomicelement.getTaxonconcept();
            Subgenus subgenusOld = persistentTaxonomicelement.getSubgenus();
            Subgenus subgenusNew = taxonomicelement.getSubgenus();
            Specificepithet specificepithetOld = persistentTaxonomicelement.getSpecificepithet();
            Specificepithet specificepithetNew = taxonomicelement.getSpecificepithet();
            Scientificnameauthorship scientificnameauthorshipOld = persistentTaxonomicelement.getScientificnameauthorship();
            Scientificnameauthorship scientificnameauthorshipNew = taxonomicelement.getScientificnameauthorship();
            Scientificname scientificnameOld = persistentTaxonomicelement.getScientificname();
            Scientificname scientificnameNew = taxonomicelement.getScientificname();
            Acceptednameusage acceptednameusageOld = persistentTaxonomicelement.getAcceptednameusage();
            Acceptednameusage acceptednameusageNew = taxonomicelement.getAcceptednameusage();
            Class class1Old = persistentTaxonomicelement.getClass1();
            Class class1New = taxonomicelement.getClass1();
            Family familyOld = persistentTaxonomicelement.getFamily();
            Family familyNew = taxonomicelement.getFamily();
            Genus genusOld = persistentTaxonomicelement.getGenus();
            Genus genusNew = taxonomicelement.getGenus();
            Infraspecificepithet infraspecificepithetOld = persistentTaxonomicelement.getInfraspecificepithet();
            Infraspecificepithet infraspecificepithetNew = taxonomicelement.getInfraspecificepithet();
            Kingdom kingdomOld = persistentTaxonomicelement.getKingdom();
            Kingdom kingdomNew = taxonomicelement.getKingdom();
            Nameaccordingto nameaccordingtoOld = persistentTaxonomicelement.getNameaccordingto();
            Nameaccordingto nameaccordingtoNew = taxonomicelement.getNameaccordingto();
            Namepublishedin namepublishedinOld = persistentTaxonomicelement.getNamepublishedin();
            Namepublishedin namepublishedinNew = taxonomicelement.getNamepublishedin();
            Nomenclaturalcode nomenclaturalcodeOld = persistentTaxonomicelement.getNomenclaturalcode();
            Nomenclaturalcode nomenclaturalcodeNew = taxonomicelement.getNomenclaturalcode();
            Order1 order1Old = persistentTaxonomicelement.getOrder1();
            Order1 order1New = taxonomicelement.getOrder1();
            Originalnameusage originalnameusageOld = persistentTaxonomicelement.getOriginalnameusage();
            Originalnameusage originalnameusageNew = taxonomicelement.getOriginalnameusage();
            Parentnameusage parentnameusageOld = persistentTaxonomicelement.getParentnameusage();
            Parentnameusage parentnameusageNew = taxonomicelement.getParentnameusage();
            Phylum phylumOld = persistentTaxonomicelement.getPhylum();
            Phylum phylumNew = taxonomicelement.getPhylum();
            List<Specimen> specimenListOld = new ArrayList<Specimen>();//persistentTaxonomicelement.getSpecimenList();
            List<Specimen> specimenListNew = new ArrayList<Specimen>();//taxonomicelement.getSpecimenList();
            if (taxonrankNew != null) {
                taxonrankNew = em.getReference(taxonrankNew.getClass(), taxonrankNew.getIdtaxonrank());
                taxonomicelement.setTaxonrank(taxonrankNew);
            }
            if (taxonomicstatusNew != null) {
                taxonomicstatusNew = em.getReference(taxonomicstatusNew.getClass(), taxonomicstatusNew.getIdtaxonomicstatus());
                taxonomicelement.setTaxonomicstatus(taxonomicstatusNew);
            }
            if (taxonconceptNew != null) {
                taxonconceptNew = em.getReference(taxonconceptNew.getClass(), taxonconceptNew.getIdtaxonconcept());
                taxonomicelement.setTaxonconcept(taxonconceptNew);
            }
            if (subgenusNew != null) {
                subgenusNew = em.getReference(subgenusNew.getClass(), subgenusNew.getIdsubgenus());
                taxonomicelement.setSubgenus(subgenusNew);
            }
            if (specificepithetNew != null) {
                specificepithetNew = em.getReference(specificepithetNew.getClass(), specificepithetNew.getIdspecificepithet());
                taxonomicelement.setSpecificepithet(specificepithetNew);
            }
            if (scientificnameauthorshipNew != null) {
                scientificnameauthorshipNew = em.getReference(scientificnameauthorshipNew.getClass(), scientificnameauthorshipNew.getIdscientificnameauthorship());
                taxonomicelement.setScientificnameauthorship(scientificnameauthorshipNew);
            }
            if (scientificnameNew != null) {
                scientificnameNew = em.getReference(scientificnameNew.getClass(), scientificnameNew.getIdscientificname());
                taxonomicelement.setScientificname(scientificnameNew);
            }
            if (acceptednameusageNew != null) {
                acceptednameusageNew = em.getReference(acceptednameusageNew.getClass(), acceptednameusageNew.getIdacceptednameusage());
                taxonomicelement.setAcceptednameusage(acceptednameusageNew);
            }
            if (class1New != null) {
                class1New = em.getReference(class1New.getClass(), class1New.getIdclass());
                taxonomicelement.setClass1(class1New);
            }
            if (familyNew != null) {
                familyNew = em.getReference(familyNew.getClass(), familyNew.getIdfamily());
                taxonomicelement.setFamily(familyNew);
            }
            if (genusNew != null) {
                genusNew = em.getReference(genusNew.getClass(), genusNew.getIdgenus());
                taxonomicelement.setGenus(genusNew);
            }
            if (infraspecificepithetNew != null) {
                infraspecificepithetNew = em.getReference(infraspecificepithetNew.getClass(), infraspecificepithetNew.getIdinfraspecificepithet());
                taxonomicelement.setInfraspecificepithet(infraspecificepithetNew);
            }
            if (kingdomNew != null) {
                kingdomNew = em.getReference(kingdomNew.getClass(), kingdomNew.getIdkingdom());
                taxonomicelement.setKingdom(kingdomNew);
            }
            if (nameaccordingtoNew != null) {
                nameaccordingtoNew = em.getReference(nameaccordingtoNew.getClass(), nameaccordingtoNew.getIdnameaccordingto());
                taxonomicelement.setNameaccordingto(nameaccordingtoNew);
            }
            if (namepublishedinNew != null) {
                namepublishedinNew = em.getReference(namepublishedinNew.getClass(), namepublishedinNew.getIdnamepublishedin());
                taxonomicelement.setNamepublishedin(namepublishedinNew);
            }
            if (nomenclaturalcodeNew != null) {
                nomenclaturalcodeNew = em.getReference(nomenclaturalcodeNew.getClass(), nomenclaturalcodeNew.getIdnomenclaturalcode());
                taxonomicelement.setNomenclaturalcode(nomenclaturalcodeNew);
            }
            if (order1New != null) {
                order1New = em.getReference(order1New.getClass(), order1New.getIdorder());
                taxonomicelement.setOrder1(order1New);
            }
            if (originalnameusageNew != null) {
                originalnameusageNew = em.getReference(originalnameusageNew.getClass(), originalnameusageNew.getIdoriginalnameusage());
                taxonomicelement.setOriginalnameusage(originalnameusageNew);
            }
            if (parentnameusageNew != null) {
                parentnameusageNew = em.getReference(parentnameusageNew.getClass(), parentnameusageNew.getIdparentnameusage());
                taxonomicelement.setParentnameusage(parentnameusageNew);
            }
            if (phylumNew != null) {
                phylumNew = em.getReference(phylumNew.getClass(), phylumNew.getIdphylum());
                taxonomicelement.setPhylum(phylumNew);
            }
            List<Specimen> attachedSpecimenListNew = new ArrayList<Specimen>();
            for (Specimen specimenListNewSpecimenToAttach : specimenListNew) {
                specimenListNewSpecimenToAttach = em.getReference(specimenListNewSpecimenToAttach.getClass(), specimenListNewSpecimenToAttach.getIdspecimen());
                attachedSpecimenListNew.add(specimenListNewSpecimenToAttach);
            }
            specimenListNew = attachedSpecimenListNew;
            taxonomicelement.setSpecimenList(specimenListNew);
            taxonomicelement = em.merge(taxonomicelement);
            if (taxonrankOld != null && !taxonrankOld.equals(taxonrankNew)) {
                taxonrankOld.getTaxonomicelementList().remove(taxonomicelement);
                taxonrankOld = em.merge(taxonrankOld);
            }
            if (taxonrankNew != null && !taxonrankNew.equals(taxonrankOld)) {
                taxonrankNew.getTaxonomicelementList().add(taxonomicelement);
                taxonrankNew = em.merge(taxonrankNew);
            }
            if (taxonomicstatusOld != null && !taxonomicstatusOld.equals(taxonomicstatusNew)) {
                taxonomicstatusOld.getTaxonomicelementList().remove(taxonomicelement);
                taxonomicstatusOld = em.merge(taxonomicstatusOld);
            }
            if (taxonomicstatusNew != null && !taxonomicstatusNew.equals(taxonomicstatusOld)) {
                taxonomicstatusNew.getTaxonomicelementList().add(taxonomicelement);
                taxonomicstatusNew = em.merge(taxonomicstatusNew);
            }
            if (taxonconceptOld != null && !taxonconceptOld.equals(taxonconceptNew)) {
                taxonconceptOld.getTaxonomicelementList().remove(taxonomicelement);
                taxonconceptOld = em.merge(taxonconceptOld);
            }
            if (taxonconceptNew != null && !taxonconceptNew.equals(taxonconceptOld)) {
                taxonconceptNew.getTaxonomicelementList().add(taxonomicelement);
                taxonconceptNew = em.merge(taxonconceptNew);
            }
            if (subgenusOld != null && !subgenusOld.equals(subgenusNew)) {
                subgenusOld.getTaxonomicelementList().remove(taxonomicelement);
                subgenusOld = em.merge(subgenusOld);
            }
            if (subgenusNew != null && !subgenusNew.equals(subgenusOld)) {
                subgenusNew.getTaxonomicelementList().add(taxonomicelement);
                subgenusNew = em.merge(subgenusNew);
            }
            if (specificepithetOld != null && !specificepithetOld.equals(specificepithetNew)) {
                specificepithetOld.getTaxonomicelementList().remove(taxonomicelement);
                specificepithetOld = em.merge(specificepithetOld);
            }
            if (specificepithetNew != null && !specificepithetNew.equals(specificepithetOld)) {
                specificepithetNew.getTaxonomicelementList().add(taxonomicelement);
                specificepithetNew = em.merge(specificepithetNew);
            }
            if (scientificnameauthorshipOld != null && !scientificnameauthorshipOld.equals(scientificnameauthorshipNew)) {
                scientificnameauthorshipOld.getTaxonomicelementList().remove(taxonomicelement);
                scientificnameauthorshipOld = em.merge(scientificnameauthorshipOld);
            }
            if (scientificnameauthorshipNew != null && !scientificnameauthorshipNew.equals(scientificnameauthorshipOld)) {
                scientificnameauthorshipNew.getTaxonomicelementList().add(taxonomicelement);
                scientificnameauthorshipNew = em.merge(scientificnameauthorshipNew);
            }
            if (scientificnameOld != null && !scientificnameOld.equals(scientificnameNew)) {
                scientificnameOld.getTaxonomicelementList().remove(taxonomicelement);
                scientificnameOld = em.merge(scientificnameOld);
            }
            if (scientificnameNew != null && !scientificnameNew.equals(scientificnameOld)) {
                scientificnameNew.getTaxonomicelementList().add(taxonomicelement);
                scientificnameNew = em.merge(scientificnameNew);
            }
            if (acceptednameusageOld != null && !acceptednameusageOld.equals(acceptednameusageNew)) {
                acceptednameusageOld.getTaxonomicelementList().remove(taxonomicelement);
                acceptednameusageOld = em.merge(acceptednameusageOld);
            }
            if (acceptednameusageNew != null && !acceptednameusageNew.equals(acceptednameusageOld)) {
                acceptednameusageNew.getTaxonomicelementList().add(taxonomicelement);
                acceptednameusageNew = em.merge(acceptednameusageNew);
            }
            if (class1Old != null && !class1Old.equals(class1New)) {
                class1Old.getTaxonomicelementList().remove(taxonomicelement);
                class1Old = em.merge(class1Old);
            }
            if (class1New != null && !class1New.equals(class1Old)) {
                class1New.getTaxonomicelementList().add(taxonomicelement);
                class1New = em.merge(class1New);
            }
            if (familyOld != null && !familyOld.equals(familyNew)) {
                familyOld.getTaxonomicelementList().remove(taxonomicelement);
                familyOld = em.merge(familyOld);
            }
            if (familyNew != null && !familyNew.equals(familyOld)) {
                familyNew.getTaxonomicelementList().add(taxonomicelement);
                familyNew = em.merge(familyNew);
            }
            if (genusOld != null && !genusOld.equals(genusNew)) {
                genusOld.getTaxonomicelementList().remove(taxonomicelement);
                genusOld = em.merge(genusOld);
            }
            if (genusNew != null && !genusNew.equals(genusOld)) {
                genusNew.getTaxonomicelementList().add(taxonomicelement);
                genusNew = em.merge(genusNew);
            }
            if (infraspecificepithetOld != null && !infraspecificepithetOld.equals(infraspecificepithetNew)) {
                infraspecificepithetOld.getTaxonomicelementList().remove(taxonomicelement);
                infraspecificepithetOld = em.merge(infraspecificepithetOld);
            }
            if (infraspecificepithetNew != null && !infraspecificepithetNew.equals(infraspecificepithetOld)) {
                infraspecificepithetNew.getTaxonomicelementList().add(taxonomicelement);
                infraspecificepithetNew = em.merge(infraspecificepithetNew);
            }
            if (kingdomOld != null && !kingdomOld.equals(kingdomNew)) {
                kingdomOld.getTaxonomicelementList().remove(taxonomicelement);
                kingdomOld = em.merge(kingdomOld);
            }
            if (kingdomNew != null && !kingdomNew.equals(kingdomOld)) {
                kingdomNew.getTaxonomicelementList().add(taxonomicelement);
                kingdomNew = em.merge(kingdomNew);
            }
            if (nameaccordingtoOld != null && !nameaccordingtoOld.equals(nameaccordingtoNew)) {
                nameaccordingtoOld.getTaxonomicelementList().remove(taxonomicelement);
                nameaccordingtoOld = em.merge(nameaccordingtoOld);
            }
            if (nameaccordingtoNew != null && !nameaccordingtoNew.equals(nameaccordingtoOld)) {
                nameaccordingtoNew.getTaxonomicelementList().add(taxonomicelement);
                nameaccordingtoNew = em.merge(nameaccordingtoNew);
            }
            if (namepublishedinOld != null && !namepublishedinOld.equals(namepublishedinNew)) {
                namepublishedinOld.getTaxonomicelementList().remove(taxonomicelement);
                namepublishedinOld = em.merge(namepublishedinOld);
            }
            if (namepublishedinNew != null && !namepublishedinNew.equals(namepublishedinOld)) {
                namepublishedinNew.getTaxonomicelementList().add(taxonomicelement);
                namepublishedinNew = em.merge(namepublishedinNew);
            }
            if (nomenclaturalcodeOld != null && !nomenclaturalcodeOld.equals(nomenclaturalcodeNew)) {
                nomenclaturalcodeOld.getTaxonomicelementList().remove(taxonomicelement);
                nomenclaturalcodeOld = em.merge(nomenclaturalcodeOld);
            }
            if (nomenclaturalcodeNew != null && !nomenclaturalcodeNew.equals(nomenclaturalcodeOld)) {
                nomenclaturalcodeNew.getTaxonomicelementList().add(taxonomicelement);
                nomenclaturalcodeNew = em.merge(nomenclaturalcodeNew);
            }
            if (order1Old != null && !order1Old.equals(order1New)) {
                order1Old.getTaxonomicelementList().remove(taxonomicelement);
                order1Old = em.merge(order1Old);
            }
            if (order1New != null && !order1New.equals(order1Old)) {
                order1New.getTaxonomicelementList().add(taxonomicelement);
                order1New = em.merge(order1New);
            }
            if (originalnameusageOld != null && !originalnameusageOld.equals(originalnameusageNew)) {
                originalnameusageOld.getTaxonomicelementList().remove(taxonomicelement);
                originalnameusageOld = em.merge(originalnameusageOld);
            }
            if (originalnameusageNew != null && !originalnameusageNew.equals(originalnameusageOld)) {
                originalnameusageNew.getTaxonomicelementList().add(taxonomicelement);
                originalnameusageNew = em.merge(originalnameusageNew);
            }
            if (parentnameusageOld != null && !parentnameusageOld.equals(parentnameusageNew)) {
                parentnameusageOld.getTaxonomicelementList().remove(taxonomicelement);
                parentnameusageOld = em.merge(parentnameusageOld);
            }
            if (parentnameusageNew != null && !parentnameusageNew.equals(parentnameusageOld)) {
                parentnameusageNew.getTaxonomicelementList().add(taxonomicelement);
                parentnameusageNew = em.merge(parentnameusageNew);
            }
            if (phylumOld != null && !phylumOld.equals(phylumNew)) {
                phylumOld.getTaxonomicelementList().remove(taxonomicelement);
                phylumOld = em.merge(phylumOld);
            }
            if (phylumNew != null && !phylumNew.equals(phylumOld)) {
                phylumNew.getTaxonomicelementList().add(taxonomicelement);
                phylumNew = em.merge(phylumNew);
            }
            for (Specimen specimenListOldSpecimen : specimenListOld) {
                if (!specimenListNew.contains(specimenListOldSpecimen)) {
                    specimenListOldSpecimen.setTaxonomicelement(null);
                    specimenListOldSpecimen = em.merge(specimenListOldSpecimen);
                }
            }
            for (Specimen specimenListNewSpecimen : specimenListNew) {
                if (!specimenListOld.contains(specimenListNewSpecimen)) {
                    Taxonomicelement oldTaxonomicelementOfSpecimenListNewSpecimen = specimenListNewSpecimen.getTaxonomicelement();
                    specimenListNewSpecimen.setTaxonomicelement(taxonomicelement);
                    specimenListNewSpecimen = em.merge(specimenListNewSpecimen);
                    if (oldTaxonomicelementOfSpecimenListNewSpecimen != null && !oldTaxonomicelementOfSpecimenListNewSpecimen.equals(taxonomicelement)) {
                        oldTaxonomicelementOfSpecimenListNewSpecimen.getSpecimenList().remove(specimenListNewSpecimen);
                        oldTaxonomicelementOfSpecimenListNewSpecimen = em.merge(oldTaxonomicelementOfSpecimenListNewSpecimen);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = taxonomicelement.getIdtaxonomicelement();
                if (findTaxonomicelement(id) == null) {
                    throw new NonexistentEntityException("The taxonomicelement with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Taxonomicelement taxonomicelement;
            try {
                taxonomicelement = em.getReference(Taxonomicelement.class, id);
                taxonomicelement.getIdtaxonomicelement();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The taxonomicelement with id " + id + " no longer exists.", enfe);
            }
            Taxonrank taxonrank = taxonomicelement.getTaxonrank();
            if (taxonrank != null) {
                taxonrank.getTaxonomicelementList().remove(taxonomicelement);
                taxonrank = em.merge(taxonrank);
            }
            Taxonomicstatus taxonomicstatus = taxonomicelement.getTaxonomicstatus();
            if (taxonomicstatus != null) {
                taxonomicstatus.getTaxonomicelementList().remove(taxonomicelement);
                taxonomicstatus = em.merge(taxonomicstatus);
            }
            Taxonconcept taxonconcept = taxonomicelement.getTaxonconcept();
            if (taxonconcept != null) {
                taxonconcept.getTaxonomicelementList().remove(taxonomicelement);
                taxonconcept = em.merge(taxonconcept);
            }
            Subgenus subgenus = taxonomicelement.getSubgenus();
            if (subgenus != null) {
                subgenus.getTaxonomicelementList().remove(taxonomicelement);
                subgenus = em.merge(subgenus);
            }
            Specificepithet specificepithet = taxonomicelement.getSpecificepithet();
            if (specificepithet != null) {
                specificepithet.getTaxonomicelementList().remove(taxonomicelement);
                specificepithet = em.merge(specificepithet);
            }
            Scientificnameauthorship scientificnameauthorship = taxonomicelement.getScientificnameauthorship();
            if (scientificnameauthorship != null) {
                scientificnameauthorship.getTaxonomicelementList().remove(taxonomicelement);
                scientificnameauthorship = em.merge(scientificnameauthorship);
            }
            Scientificname scientificname = taxonomicelement.getScientificname();
            if (scientificname != null) {
                scientificname.getTaxonomicelementList().remove(taxonomicelement);
                scientificname = em.merge(scientificname);
            }
            Acceptednameusage acceptednameusage = taxonomicelement.getAcceptednameusage();
            if (acceptednameusage != null) {
                acceptednameusage.getTaxonomicelementList().remove(taxonomicelement);
                acceptednameusage = em.merge(acceptednameusage);
            }
            Class class1 = taxonomicelement.getClass1();
            if (class1 != null) {
                class1.getTaxonomicelementList().remove(taxonomicelement);
                class1 = em.merge(class1);
            }
            Family family = taxonomicelement.getFamily();
            if (family != null) {
                family.getTaxonomicelementList().remove(taxonomicelement);
                family = em.merge(family);
            }
            Genus genus = taxonomicelement.getGenus();
            if (genus != null) {
                genus.getTaxonomicelementList().remove(taxonomicelement);
                genus = em.merge(genus);
            }
            Infraspecificepithet infraspecificepithet = taxonomicelement.getInfraspecificepithet();
            if (infraspecificepithet != null) {
                infraspecificepithet.getTaxonomicelementList().remove(taxonomicelement);
                infraspecificepithet = em.merge(infraspecificepithet);
            }
            Kingdom kingdom = taxonomicelement.getKingdom();
            if (kingdom != null) {
                kingdom.getTaxonomicelementList().remove(taxonomicelement);
                kingdom = em.merge(kingdom);
            }
            Nameaccordingto nameaccordingto = taxonomicelement.getNameaccordingto();
            if (nameaccordingto != null) {
                nameaccordingto.getTaxonomicelementList().remove(taxonomicelement);
                nameaccordingto = em.merge(nameaccordingto);
            }
            Namepublishedin namepublishedin = taxonomicelement.getNamepublishedin();
            if (namepublishedin != null) {
                namepublishedin.getTaxonomicelementList().remove(taxonomicelement);
                namepublishedin = em.merge(namepublishedin);
            }
            Nomenclaturalcode nomenclaturalcode = taxonomicelement.getNomenclaturalcode();
            if (nomenclaturalcode != null) {
                nomenclaturalcode.getTaxonomicelementList().remove(taxonomicelement);
                nomenclaturalcode = em.merge(nomenclaturalcode);
            }
            Order1 order1 = taxonomicelement.getOrder1();
            if (order1 != null) {
                order1.getTaxonomicelementList().remove(taxonomicelement);
                order1 = em.merge(order1);
            }
            Originalnameusage originalnameusage = taxonomicelement.getOriginalnameusage();
            if (originalnameusage != null) {
                originalnameusage.getTaxonomicelementList().remove(taxonomicelement);
                originalnameusage = em.merge(originalnameusage);
            }
            Parentnameusage parentnameusage = taxonomicelement.getParentnameusage();
            if (parentnameusage != null) {
                parentnameusage.getTaxonomicelementList().remove(taxonomicelement);
                parentnameusage = em.merge(parentnameusage);
            }
            Phylum phylum = taxonomicelement.getPhylum();
            if (phylum != null) {
                phylum.getTaxonomicelementList().remove(taxonomicelement);
                phylum = em.merge(phylum);
            }
            List<Specimen> specimenList = taxonomicelement.getSpecimenList();
            for (Specimen specimenListSpecimen : specimenList) {
                specimenListSpecimen.setTaxonomicelement(null);
                specimenListSpecimen = em.merge(specimenListSpecimen);
            }
            em.remove(taxonomicelement);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Taxonomicelement> findTaxonomicelementEntities() {
        return findTaxonomicelementEntities(true, -1, -1);
    }

    public List<Taxonomicelement> findTaxonomicelementEntities(int maxResults, int firstResult) {
        return findTaxonomicelementEntities(false, maxResults, firstResult);
    }

    private List<Taxonomicelement> findTaxonomicelementEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Taxonomicelement.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Taxonomicelement findTaxonomicelement(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Taxonomicelement.class, id);
        } finally {
            em.close();
        }
    }

    public int getTaxonomicelementCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Taxonomicelement> rt = cq.from(Taxonomicelement.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
